import plotly.express as px
import pandas as pd
Lectura de datos y creacion del Dataframe#
url_co2 = 'https://raw.githubusercontent.com/lihkir/Uninorte/main/AppliedStatisticMS/DataVisualizationRPython/Lectures/Python/PythonDataSets/co2.csv'
co2 = pd.read_csv(url_co2)
url_gm = 'https://raw.githubusercontent.com/lihkir/Uninorte/main/AppliedStatisticMS/DataVisualizationRPython/Lectures/Python/PythonDataSets/gapminder.csv'
gm = pd.read_csv(url_gm)
df_gm = gm[['Country', 'region']].drop_duplicates()
df_w_regions = pd.merge(co2, df_gm, left_on='country', right_on='Country', how='inner')
df_w_regions = df_w_regions.drop('Country', axis='columns')
new_co2 = pd.melt(df_w_regions, id_vars=['country', 'region'])
columns = ['country', 'region', 'year', 'co2']
new_co2.columns = columns
df_co2 = new_co2[new_co2['year'].astype('int64') > 1963]
df_co2 = df_co2.sort_values(by=['country', 'year'])
df_co2['year'] = df_co2['year'].astype('int64')
df_gdp = gm[['Country', 'Year', 'gdp']]
df_gdp.columns = ['country', 'year', 'gdp']
data = pd.merge(df_co2, df_gdp, on=['country', 'year'], how='left')
data = data.dropna()
data.head()
| country | region | year | co2 | gdp | |
|---|---|---|---|---|---|
| 0 | Afghanistan | South Asia | 1964 | 0.0863 | 1182.0 |
| 1 | Afghanistan | South Asia | 1965 | 0.1010 | 1182.0 |
| 2 | Afghanistan | South Asia | 1966 | 0.1080 | 1168.0 |
| 3 | Afghanistan | South Asia | 1967 | 0.1240 | 1173.0 |
| 4 | Afghanistan | South Asia | 1968 | 0.1160 | 1187.0 |
Primer Plot#
-gráfico de dispersión con los ejes x e y como year y co2 respectivamente. Añada uno para los valores de co2 con el parámetro marginal_y.
xmin,xmax = min(data.year),max(data.year)
ymin,ymax = min(data.co2),max(data.co2)
fig = px.scatter(data,
x="year", y="co2",
animation_group="country",
color="region",
hover_name="country",
marginal_y="box",
range_x=[xmin,xmax],
range_y=[ymin,ymax])
fig.show()
Segundo plot#
-Genere un gráfico de caja para los valores del PIB con el parámetro marginal_x. Añada los parámetros de animación en la columna del año
Genere un gráfico de dispersión con los ejes x e y como gdp y co2 respectivamente.
xmin,xmax = min(data.gdp),max(data.gdp)
ymin,ymax = min(data.co2),max(data.co2)
fig = px.scatter(data,
x="gdp", y="co2",
animation_group="country",
color="region",
hover_name="country",
animation_frame="year",
marginal_y="box",
marginal_x="box",
range_x=[xmin,xmax],
range_y=[ymin,ymax])
fig.show()
Tercer Plot#
xmin,xmax = min(data.gdp),max(data.gdp)
ymin,ymax = min(data.co2),max(data.co2)
fig = px.density_contour(data,
x="gdp", y="co2",
animation_group="country",
color="region",
hover_name="country",
animation_frame="year",
marginal_y="box",
marginal_x="box",
range_x=[xmin,xmax],
range_y=[ymin,ymax])
fig.show()